%%%%%%%%%%%%%%%%%%%%%%%%%%  ??????  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clc;clear all;close all;

%%%%%%%%%%%%%%  ??????????????????????????????????  %%%%%%%%%%%%%%%%%%

% ??????????????????????????
t=0:1/100:9;
I1=sin(t); 
I2=randn(1,901);
I3=square(4*t);
subplot(4,3,1),plot(I1),title('Input Signal 1'),
subplot(4,3,2),plot(I2),title('Input Signal 2'),
subplot(4,3,3),plot(I3),title('Input Signal 3'),

% ????????????
S=[I1;I2;I3];                          % ??????????????????????????????????????????
                                       % ????S_all??????????????????????????????

Sweight=randn(size(S,1));              % ??????????????????????????????????
MixedS=Sweight*S;                      % ????????????????????

% ????????????????????????
subplot(4,3,4),plot(MixedS(1,:)),title('Mixed Signal 1'),
subplot(4,3,5),plot(MixedS(2,:)),title('Mixed Signal 2'),
subplot(4,3,6),plot(MixedS(3,:)),title('Mixed Signal 3'),

MixedS_bak=MixedS;                      % ????????????????????????????????????????
%%%%%%%%%%%%%%%%%%%%%%%%%%  ??????  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MixedS_mean=zeros(3,1);
for i=1:3
    MixedS_mean(i)=mean(MixedS(i,:));
end                                        % ????MixedS??????

for i=1:3
    for j=1:size(MixedS,2)
        MixedS(i,j)=MixedS(i,j)-MixedS_mean(i);
    end
end                                       

%%%%%%%%%%%%%%%%%%%%%%%%%%%  ????  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

MixedS_cov=cov(MixedS');                    % cov????????????????
[E,D]=eig(MixedS_cov);                      % ????????????????????????????????????
Q=inv(sqrt(D))*(E)';                        % Q??????????
MixedS_white=Q*MixedS;                      % MixedS_white??????????????????
IsI=cov(MixedS_white');                     % IsI??????????            

%%%%%%%%%%%%%%%%%%%%%%%%??FASTICA????  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X=MixedS_white;                            % ????????????X????????
[VariableNum,SampleNum]=size(X);
numofIC=VariableNum;                       % ??????????????????????????????????
B=zeros(numofIC,VariableNum);              % ????????????w??????????,B=[b1  b2  ...   bd]
for r=1:numofIC                            % ????????????????????
    i=1;maxIterationsNum=100;              % ????????????????????????????????????????????????????????????
    IterationsNum=0;
    b=rand(numofIC,1)-.5;                  % ????????b????
    b=b/norm(b);                           % ??b??????
    while i<=maxIterationsNum+1
        if i == maxIterationsNum           % ????????????
            fprintf('\n??%d??????%d??????????????????', r,maxIterationsNum);
            break;
        end
        bOld=b;                      
        a2=1;
        u=1;
        t=X'*b;
        g=(exp(2.*t)-1)./(exp(2.*t)+1);
        dg=4*exp(2.*t)./(exp(2.*t)+1).^2;
        b=((1-u)*t'*g*b+u*X*g)/SampleNum-mean(dg)*b;
                                            % ??????????????????????????2.52
        b=b-B*B'*b;                         % ??b??????
        b=b/norm(b); 
        if abs(abs(b'*bOld)-1)<1e-9         % ????????????????b
               B(:,r)=b;
             break;
         end
        i=i+1;        
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%  ??????????????  %%%%%%%%%%%%%%%%%%%%%%%%

ICAedS=B'*Q*MixedS_bak;                     % ????????????????2.55

% ????????????????????????
subplot(4,3,7),plot(ICAedS(1,:)),title('ICA Output 1'),
subplot(4,3,8),plot(ICAedS(2,:)),title('ICA Output 2'),
subplot(4,3,9),plot(ICAedS(3,:)),title('ICA Output 3'),


%%%%%%%%%%%%%%%%%%%%%%%%%  PCA??????????  %%%%%%%%%%%%%%%%%%%%%%%%
[V,D]=eig(MixedS_cov);
Vtmp=zeros(size(V,1),1);
for j=1:2
    for i=1:2
        if D(i,i)<D(i+1,i+1)
            tmp=D(i,i);Vtmp=V(:,i);
            D(i,i)=D(i+1,i+1);V(:,i)=V(:,i+1);
            D(i+1,i+1)=tmp;V(:,i+1)=Vtmp;
        end
    end
end

t1=(MixedS'*V(:,1))';
t2=(MixedS'*V(:,2))';   
t3=(MixedS'*V(:,3))';

subplot(4,3,10),plot(t1),title('PCA Output 1'),
subplot(4,3,11),plot(t2),title('PCA Output 2'),
subplot(4,3,12),plot(t3),title('PCA Output 3'),